package com.heima.article.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;


    /**
     * 加载首页
     *
     * @param dto
     */
    @Override
    public ResponseResult load(Short type, ArticleHomeDto dto) {
        //参数校验
        if(dto == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //数据处理
        if(type != ArticleConstants.LOAD_MORE && type != ArticleConstants.LOAD_NEW)//加载类型
            type = ArticleConstants.LOAD_MORE;
        if(dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());
        if(dto.getSize() == null || dto.getSize() <= 0 || dto.getSize() > 50)
            dto.setSize(10);
        if(StringUtils.isEmpty(dto.getTag())) dto.setTag(ArticleConstants.DEFAULT_TAG);

        //数据查询
        List<ApArticle> articleList = articleMapper.loadList(type,dto);

        //封装数据返回
        return ResponseResult.okResult(articleList);
    }

}
